package a日常研究._03java线程._01生产者消费者模型;

import a日常研究._03java线程._01生产者消费者模型.base.BasePCModel;
import a日常研究._03java线程._01生产者消费者模型.entity.Product;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 多个生产者，消费者模型
 */
public class MultiplePCModel extends BasePCModel {
    public static void main(String[] args) {
        new MultiplePCModel().testMultiPCUseBlockQueue();
    }

    /**
     * 多个生产者，多个消费者，使用对象锁
     */
    public void testMultiPCUseObjectLock() {
        Object lock = new Object();
        Runnable pRun = () -> {
            while (true) {
                synchronized (lock) {
                    try {
                        while (queue.size() == FULL_NUM) {
                            lock.wait();
                        }
                        produce();
                        lock.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Runnable cRun = () -> {
            while (true) {
                synchronized (lock) {
                    try {
                        while (queue.isEmpty()) {
                            lock.wait();
                        }
                        consume();
                        lock.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        startAll(cRun, pRun);
    }

    /**
     * 多个生产者，多个消费者，使用Lock
     */
    public void testMultiPCUseJUCLock() {
        Lock lock = new ReentrantLock();
        // 可做到对生产者和消费者进行分组，进行针对组的精确唤醒
        Condition proCon = lock.newCondition(); // 生产者锁
//        Condition conCon = lock.newCondition(); // 消费者锁
        Runnable pRun = () -> {
            while (true) {
                try {
                    lock.lock();
                    while (queue.size() == FULL_NUM) {
                        proCon.await();
                    }
                    produce();
//                    conCon.signalAll();
                    proCon.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        };
        Runnable cRun = () -> {
            while (true) {
                try {
                    lock.lock();
                    while (queue.isEmpty()) {
                        proCon.await();
//                        conCon.await();
                    }
                    consume();
                    proCon.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        };
        startAll(cRun, pRun);
    }


    /**
     * 多个生产者，多个消费者，会用blockingQueue
     */
    public void testMultiPCUseBlockQueue() {
        BlockingQueue<Product> blockingQueue = new ArrayBlockingQueue<>(FULL_NUM);
        AtomicInteger integer = new AtomicInteger(1);
        Runnable pRun = () -> {
            try {
                while (true) {
                    Product product = new Product("第" + integer.getAndIncrement() + "个产品");
                    blockingQueue.put(product);
                    System.out.println(Thread.currentThread().getName() + "生产者生产" + product.name);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        };

        Runnable cRun = () -> {
            try {
                while (true) {
                    Product product = blockingQueue.take();
                    System.out.println(Thread.currentThread().getName() + "消费者消费" + product.name);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        startAll(cRun, pRun);
    }

    public void startAll(Runnable cRun, Runnable pRun) {
        Thread p1 = new Thread(pRun);
        p1.setName("生产者01");
        Thread p2 = new Thread(pRun);
        p2.setName("生产者02");
        Thread p3 = new Thread(pRun);
        p3.setName("生产者03");

        Thread c1 = new Thread(cRun);
        c1.setName("消费者01");
        Thread c2 = new Thread(cRun);
        c2.setName("消费者02");
        Thread c3 = new Thread(cRun);
        c3.setName("消费者03");
        p1.start();
        p2.start();
        p3.start();
        c1.start();
        c2.start();
        c3.start();
    }
}
